<script lang="ts" setup>
	import { reactive, ref } from "vue";
	import orderList from '@/components/order-list/order-list.vue'
	import {
		onLoad, onUnload, onReachBottom,
		onShow
	} from '@dcloudio/uni-app';
	import {
		getCurrentInstance
	} from 'vue'
	import router from "../../router";
	const {
		proxy,
	} = getCurrentInstance()
	onLoad((option) => {
		status.value = option.id
		tabsCurrent.value = option.current
		// getList()
	})
	onShow(() => {
		page.value = 1
		listData.value = []
		getList()
	})
	let sendBackId = ref('')
	const tabsCurrent = ref(0)
	let tabList = ref([{
		name: '全部',
		id: null,
		sId: 0
	}, {
		name: '待付款',
		id: 1,
		sId: 0
	}, {
		name: '待发货',
		id: 2,
		sId: 0
	}, {
		name: '待收货',
		id: 3,
		sId: 0
	}, {
		name: '待寄还',
		id: 4,
		sId: 2
	}, {
		name: '待评价',
		id: 4,
		sId: 0
	}, {
		name: '售后',
		id: -1,
		sId: 0
	}])
	let active = ref({
		fontSize: '28rpx',
		// width: '130rpx',
		height: '30rpx',
		color: '#CB645E',
		textAlign: 'center',
		fontWeight: '500',
	})
	let unActive = ref({
		fontSize: '28rpx',
		fontWeight: '400',
		// width: '120rpx',
		height: '30rpx',
		textAlign: 'center',
		color: '#333',
	})
	let status = ref(null)
	let page = ref(1)
	let listData = ref([])
	const tabsClick = (e) => {
		if (e.sId == 2) {
			sendBackId.value = 2
		} else {
			sendBackId.value = ''
		}
		page.value = 1
		listData.value = []
		console.log(e);
		status.value = e.id
		getList()
	}
	const getList = () => {
		proxy.$req('GET', proxy.$api.getOrderListUser, {
			status: status.value,
			sendbackStatus: sendBackId.value,
			pageNo: page.value,
			pageSize: 10
		}).then((res) => {
			console.log(res, '订单datares');
			listData.value = listData.value.concat(res.result.records)
		})
	}
	onReachBottom(() => {
		page.value++
		getList()
	})
	const closeOrder = (e) => {
		//取消订单
		console.log(e);
		proxy.$req('POST', proxy.$api.orderEditShop, {
			id: e.id,
			status: -2
		}).then((res) => {
			console.log(res);
			uni.$u.toast(res.message)
			if (res.success) {
				listData.value = []
				page.value = 1
				getList()
			}
		})
	}
	const nowPay = (e) => {
		//立即支付
		// #ifdef APP-PLUS
		plus.runtime.openURL(e.url)
		console.log('app');
		// #endif
		// #ifdef H5
		window.open(e.url)
		console.log('h5');
		// #endif
	}
	const kefu = (e) => {
		//联系客服

	}
	const confirmShou = (e) => {
		//确认收货
		console.log(e);
		proxy.$req('POST', proxy.$api.userConfirmShou, {
			id: e.id,
			// status: 4
		}).then((res) => {
			console.log(res);
			uni.$u.toast(res.message)
			if (res.success) {
				listData.value = []
				page.value = 1
				getList()
			}
		})
	}
	const deleteOrder = (e) => {
		//删除订单
		console.log(e);
		proxy.$req('DELETE', proxy.$api.deleteOrder, {
			id: e.id,
		}).then((res) => {
			console.log(res);
			uni.$u.toast(res.message)
			if (res.success) {
				listData.value = []
				page.value = 1
				getList()
			}
		})
	}
	const jihuan = (e) => {
		//寄还
		// router.push({
		// 	name: 'applyForJihuan',
		// 	params: {
		// 		id: e.id,
		// 		item: encodeURIComponent(JSON.stringify(e.item)),
		// 		all: 1
		// 	}
		// })

	}
	const closeShou = (e) => {
		//取消售后
		console.log(e);
		proxy.$req('POST', proxy.$api.closeShou, {
			id: e.id,
			refundStatus: -1
		}).then((res) => {
			console.log(res);
			uni.$u.toast(res.message)
			if (res.success) {
				listData.value = []
				page.value = 1
				getList()
			}
		})
	}
</script>
<template>
	<view class="tabsBox">
		<u-tabs :list="tabList" :activeStyle="active" :current="tabsCurrent" lineColor="#CB645E"
			:inactiveStyle="unActive" @click="tabsClick"></u-tabs>
	</view>
	<order-list :listData='listData' @closeOrder="closeOrder" @nowPay="nowPay" @kefu="kefu" @confirmShou="confirmShou"
		@jihuan="jihuan" @closeShou="closeShou" @deleteOrder="deleteOrder"></order-list>
</template>
<style lang="scss">
	page {
		background-color: #FAFAFA;
	}

	.tabsBox {
		width: 100%;
		height: 72rpx;
		background-color: #fff;
		margin-bottom: 16rpx;
	}
</style>